using System.ComponentModel;
using System;
namespace Lizk.SimpleHUD
{
#if !XBOX
    [TypeConverterAttribute(typeof(GeometryConverter))]
#endif
    [Serializable]
    public struct RectangleS
    {
        private float x, y, width, height;

        public float X
        {
            get { return x; }
            set { x = value; }
        }

        public float Y
        {
            get { return y; }
            set { y = value; }
        }

        public float Width
        {
            get { return width; }
            set { width = value; }
        }

        public float Height
        {
            get { return height; }
            set { height = value; }
        }

#if !XBOX
        [Browsable(false)]
#endif
        public float Top { get { return y; } }
#if !XBOX
        [Browsable(false)]
#endif
        public float Left { get { return x; } }
#if !XBOX
        [Browsable(false)]
#endif
        public float Right { get { return x + width; } }
#if !XBOX
        [Browsable(false)]
#endif
        public float Bottom { get { return y + height; } }


#if !XBOX
        [Browsable(false)]
#endif
        public SizeS Size
        {
            get { return new SizeS(width, height); }
            set
            {
                width = value.Width;
                height = value.Height;
            }
        }

      #if !XBOX
  [Browsable(false)]
#endif
        public PointS Location
        {
            get { return new PointS(x, y); }
            set
            {
                x = value.X;
                y = value.Y;
            }
        }

        public RectangleS(PointS point, SizeS size)
        {
            x = point.X;
            y = point.Y;
            width = size.Width;
            height = size.Height;
        }

        public RectangleS(float x, float y, float width, float height)
        {
            this.width = width;
            this.height = height;
            this.x = x;
            this.y = y;
        }

        public static RectangleS operator +(RectangleS a, SizeS b)
        {
            return new RectangleS(a.x, a.y, a.width + b.Width, a.height + b.Height);
        }

        public static RectangleS operator -(RectangleS a, SizeS b)
        {
            return new RectangleS(a.x, a.y, a.width - b.Width, a.height - b.Height);
        }

        public static RectangleS operator +(RectangleS a, PointS b)
        {
            return new RectangleS(a.x + b.X, a.y + b.Y, a.width, a.height);
        }

        public static RectangleS operator -(RectangleS a, PointS b)
        {
            return new RectangleS(a.x - b.X, a.y - b.Y, a.width, a.height);
        }

        public static RectangleS operator +(PointS b, RectangleS a)
        {
            return new RectangleS(a.x + b.X, a.y + b.Y, a.width, a.height);
        }

        public static RectangleS operator -(PointS b, RectangleS a)
        {
            return new RectangleS(a.x - b.X, a.y - b.Y, a.width, a.height);
        }

        public static bool operator ==(RectangleS a, RectangleS b)
        {
            return a.Equals(b);
        }

        public static bool operator !=(RectangleS a, RectangleS b)
        {
            return !a.Equals(b);
        }

        public override bool Equals(object obj)
        {
            if (!(obj is RectangleS))
                return false;

            RectangleS b = (RectangleS) obj;
            return width == b.width && height == b.height && b.x == x && y == b.y;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        

        public static RectangleS Empty { get { return new RectangleS(0, 0, 0, 0); } }

        public static RectangleS Intersect(RectangleS box, RectangleS box2)
        {
            float left3, right3, top3, bottom3;

            if (box2.Top > box.Bottom || box.Top > box2.Bottom || box2.Right < box.Left || box.Right < box2.Left)
                return Empty;

            left3 = box2.Left > box.Left ? box2.Left : box.Left;
            right3 = box2.Right < box.Right ? box2.Right : box.Right;
            top3 = box2.Top > box.Top ? box2.Top : box.Top;
            bottom3 = box2.Bottom < box.Bottom ? box2.Bottom : box.Bottom;

            return new RectangleS(left3, top3, right3 - left3, bottom3 - top3);
        }

        public bool Contains(PointS p)
        {
            return p.X >= this.Left && p.X <= this.Right && p.Y >= this.Top && p.Y <= this.Bottom;

        }

        public bool Contains(RectangleS p)
        {
            return p.Left >= this.Left && p.Right <= this.Right && p.Top >= this.Top && p.Bottom <= this.Bottom;

        }

    }
}